<!-- 首页 -->
<template>
	<view class="page-index-index" v-if="ishaids">
		<view class="logincenter">
			<image class="logos" src="../../static/images/applogo.png" mode=""></image>
			<view class="center_input">
				<u-input class="inputs" v-model="userName" type="text" placeholder="请输入账号" />
				<u-input class="inputs" v-model="password" type="password" placeholder="请输入密码" />
			</view>
			<view class="okbtns" @click="topaesad">
				登录
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				userName: "",
				password: "",
				ishaids: false,
				obJs: null,
				id: null,
				code: null,
			}
		},
		onLoad(options) {
			// if(options.code){
			// 	this.id = options.id
			// }
			// if(options.code){
			// 	this.code = options.code
			// }
			uni.clearStorage();
			this.getCode()

		},
		onShareAppMessage(res) {
			return {
				title: '',
				path: "/pages/index/index"
			}
		},
		onShow() {},
		onReachBottom() {},
		methods: {
			topaesad() {
				// let str =  'eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyX25hbWUiOiJ1c2VyczQ0NTU1NzEwODMxMDY3MTM2MiIsInNjb3BlIjpbImFwcCJdLCJpZCI6NTEsImV4cCI6MTY5Mzc2MDg5NSwidXNlck5hbWUiOiJ1c2VyczQ0NTU1NzEwODMxMDY3MTM2MiIsImF1dGhvcml0aWVzIjpbInBhcmVudCJdLCJqdGkiOiIxNzlkNGZhZi0xMzRlLTQ5N2YtYTUzNi03Yjg4MWQ1MGY0ZTQiLCJjbGllbnRfaWQiOiJkZXZXZWIifQ.UO0gb8ePLNWv-RxcrXyrblb_zmGoCGfioYvvWAe1N_bsDJtLhdCNyWuh-M9kWtKr9m6i6YcoeW3WU2QmmQUO61obxOIUYY7q9jMcvLKPF8OV0wfM3MFOUNxCjgdsOy6KweugSmUSq2Gd9VTRjI5anWMEettHNgp54u2Ty_HRUIYDyjABmceGJUq6QIVYbt_oVzORKITw1taLwniBXLdrbxJDV4dUQw-bFo7CV8uskJe2bUdMkDjWcVu44kTDs3tMtgu8jY18qwLIu15bmQRxjGleI2e05-ztL8Ll4m8TPmSuDPoJO84TLsUFeNll7ywVySnMHdT109TYbXdJNBCHdg';
				// uni.$func.cache("token", str)
				// uni.$api.common.userscurrent().then((res) => {
				// 	uni.$func.cache("userInfo", res.result)
				// 	uni.switchTab({
				// 		url: "./indexlist"
				// 	})
				// });
				uni.$api.common.mobilegzhtoken({
					password: this.password,
					userName: this.userName
				}).then((res) => {
					uni.showToast({
						title: res.msg,
						icon: 'none'
					})
					uni.$func.cache("token", res.result.access_token)
					
					uni.$api.common.userscurrent().then((res) => {
						uni.$func.cache("userInfo", res.result)
						uni.switchTab({
							url: "./indexlist"
						})
					});

				});


			},

			getCode() { // 非静默授权，第一次有弹框
				this.id = this.getUrlCode().id
				var local = 'https://www.fbty.tech/h5?id=' + this.id //当前页面的地址
				// const local = window.location.href;
				var appid = 'wxf84dbbafd7043c5d' //公众号里有自己查，不会找的查一下百度
				this.code = this.getUrlCode().code // 截取code	
				// 判断地址栏参数有无code,如果没有code，页面地址就跳转到微信提供的获取code的链接
				if (this.code == null || this.code == '') {
					let url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=" + appid +
						"&redirect_uri=" +
						encodeURIComponent(local) +
						"&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect";
					console.log(url)
					location.href = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=" + appid +
						"&redirect_uri=" +
						encodeURIComponent(local) + "&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect"

				} else {
					if (this.id && this.id != 'undefined') {
						uni.$api.common.oauthwechatAuth({
							code: this.code,
							studentId: this.id
						}).then((res) => {
							console.log(res)
							uni.$func.cache("token", res.result.oAuth2AccessToken.access_token)
							this.obJs = res.result
							if (this.obJs.roleId == 3) {
								uni.$api.common.userscurrent().then((res) => {
									uni.$func.cache("userInfo", res.result)
									uni.switchTab({
										url: "./indexlist"
									})
								});
							} else {
								this.ishaids = true
							}
						});
					} else {
						// 获取code后自己的业务逻辑
						uni.$api.common.oauthwechatAuth({
							code: this.code,
							studentId: ''
						}).then((res) => {

							uni.$func.cache("token", res.result.oAuth2AccessToken.access_token)
							this.obJs = res.result
							if (this.obJs.roleId == 3) {
								uni.$api.common.userscurrent().then((res) => {
									uni.$func.cache("userInfo", res.result)
									uni.switchTab({
										url: "./indexlist"
									})
								});
							} else {
								this.ishaids = true
							}
						});
					}

				}
			},
			// 截取url中的code方法
			getUrlCode() {
				var url = location.search

				var theRequest = new Object()
				if (url.indexOf("?") != -1) {
					var str = url.substr(1)
					var strs = str.split("&")
					for (var i = 0; i < strs.length; i++) {
						theRequest[strs[i].split("=")[0]] = (strs[i].split("=")[1])
					}
				}
				console.log(theRequest, '111')
				return theRequest
			},
		}
	}
</script>

<style lang="scss">
	.logincenter {
		text-align: center;
		padding: 200px 80rpx;

		.logos {
			width: 300rpx;
			height: 300rpx;
			margin-bottom: 100rpx;
		}

		.center_input {
			.inputs {
				// display: block;
				width: 100%;
				height: 80rpx;
				border-bottom: 1rpx solid #333333;
				margin-bottom: 80rpx;
			}
		}

		.okbtns {
			width: 100%;
			padding: 20rpx 0;
			border-radius: 50rpx;
			background-color: #E62131;
			color: #ffffff;
			font-size: 36rpx;
		}
	}
</style>